package com.zhh.dataservice.service;

import com.zhh.api.model.ProductInfo;
import com.zhh.api.pojo.MultiProduct;
import com.zhh.api.service.ProductService;
import com.zhh.common.contants.FMContants;
import com.zhh.common.util.CommonUtil;
import com.zhh.dataservice.mapper.ProductInfoMapper;
import org.apache.curator.framework.recipes.queue.MultiItem;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@DubboService(interfaceClass = ProductService.class,version = "1.0")
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductInfoMapper productInfoMapper;

    //首页的产品展示，分页查询某种产品的详细信息
    @Override
    public MultiProduct queryIndexPageProduct() {
        MultiProduct multiProduct = new MultiProduct();

        //分页查询出一条新手包，三条优选和三条散标
        List<ProductInfo> xinShouBaoList = productInfoMapper.selectByTypeLimit(FMContants.PRODUCT_TYPE_XINSHOUBAO,0,1);
        List<ProductInfo> youXuanList = productInfoMapper.selectByTypeLimit(FMContants.PRODUCT_TYPE_YOUXUAN,0,3);
        List<ProductInfo> sanBiaoList = productInfoMapper.selectByTypeLimit(FMContants.PRODUCT_TYPE_SANBIAO,0,3);

        //把记录存在pojo类中
        multiProduct.setXinShouBao(xinShouBaoList);
        multiProduct.setYouXuan(youXuanList);
        multiProduct.setSanBiao(sanBiaoList);

        return multiProduct;
    }

    //根据产品类型，查询该类型所有产品，支持分页
    @Override
    public List<ProductInfo> queryByTypeLimit(Integer pType, Integer pageNo, Integer pageSize) {

        List<ProductInfo> productInfos = new ArrayList<>();
        if( pType==0 || pType==1 || pType==2) {
            pageNo = CommonUtil.defaultPageNo(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            int offset = (pageNo - 1) * pageSize;
            productInfos = productInfoMapper.selectByTypeLimit(pType, offset, pageSize); //产品类型，从第几个开始，一页有几个
        }
        return productInfos;
    }

    //某种产品的总数
    @Override
    public Integer queryRecordNumsByType(Integer pType) {

        Integer counts = 0;

        if( pType==0 || pType==1 || pType==2) {
            counts = productInfoMapper.selectCountByType(pType);
        }
        return counts;
    }

    //根据id，查询某种产品的详细信息
    @Override
    public ProductInfo queryById(Integer id) {
        ProductInfo productInfo = null;
        if(id != null && id >0){
            productInfo = productInfoMapper.selectByPrimaryKey(id);
        }
        return productInfo;
    }

    @Override
    public Integer queryTypeById(Integer id) {
        Integer type = null;
        if(id != null){
            type = productInfoMapper.selectTypeById(id);
        }
        return type;
    }
}
